#include "stdafx.h"
#include "common.h"

void InitFileInfo(FileInfo_t* pstFileInfo)
{
	pstFileInfo->eUsedStatus = STATUS_UNUSED;
	pstFileInfo->nItemIndex = -1L;
	pstFileInfo->eDocType = DOC_TYPE_UNKNOWN;

#ifdef _UNICODE
	int i(0);
	for ( i = 0; i < MAX_PATH; ++i )
		pstFileInfo->szFilePath[i] = 0x00;

	for ( i = 0; i < FILENAME_MAX; ++i )
		pstFileInfo->szFileName[i]= 0x00;
#else
	ZeroMemory(pstFileInfo->szFilePath, MAX_PATH);
	ZeroMemory(pstFileInfo->szFileName, FILENAME_MAX);
#endif

	pstFileInfo->hProgress1 = (HWND)NULL;
	pstFileInfo->hProgress2 = (HWND)NULL;

	pstFileInfo->strErr.Empty();
}


void ReleaseList(FILEINFOLST& vecLst)
{
	// release all progress window handles in list control
	// m_myLstCtrl.ReleaseItems();


	// release memory resource	
	FILEINFO_ITERATOR pos = vecLst.begin();
	while ( pos != vecLst.end() )
	{
		delete (*pos);
		++pos;
	}

	vecLst.clear();
}

void GetErrMsg(FILEINFOLST& vecLst, CString& strErr)
{
	FILEINFO_ITERATOR pos = vecLst.begin();
	while ( pos != vecLst.end() )
	{
		strErr = strErr + (*pos)->strErr;
		++pos;
	}
}


void ResetUnused(FILEINFOLST& vecLst)
{
	FILEINFO_ITERATOR pos = vecLst.begin();
	while ( pos != vecLst.end() )
	{
		(*pos)->eUsedStatus = STATUS_UNUSED;
		
		++pos;
	}
}


FileInfo_t* GetNextUnused(FILEINFOLST& vecLst)
{
	FILEINFO_ITERATOR pos = vecLst.begin();
	while ( pos != vecLst.end() )
	{
		if ( STATUS_UNUSED == (*pos)->eUsedStatus )
		{
			return (*pos);
		}
			
		++pos;
	}
	
	return NULL;
}


BOOL IsAllFinished(FILEINFOLST& vecLst)
{
	FILEINFO_ITERATOR pos = vecLst.begin();
	while ( pos != vecLst.end() )
	{
		if ( STATUS_FININISHED != (*pos)->eUsedStatus )
		{
			return FALSE;
		}
			
		++pos;
	}

	return TRUE;
}


/*----------------------------------------------------------
Purpose: Returns TRUE if the file/directory exists.

Returns: see above
Condition:    --
*/
BOOL PathExists(LPCTSTR pszPath)
{
    return GetFileAttributes(pszPath) != 0xFFFFFFFF;
}

long CreateFolders(LPCTSTR pcszPath)
{
	/* Does the folder exist? */
	
	if ( !PathExists(pcszPath) )
	{
		/* No.  Try to create it. */
		
		if ( CreateDirectory(pcszPath, NULL) )
			return 0L;
		else
			return -1L;
	}
	
	return 0L;	
}
